<?php
/**
 * @Copyright   2008, XXXX-XXXX
 * All rights reserved. 
 * feature summary.
 * @filesource  data_handler.php 
 * @author      lipeng <lipengage@gmain.com> 
 * @version     $Id: $ 
 * @package     application
 * @subpackage  service
 */

/*
 * a base class handle the request
 */
abstract class data_handler {
	protected static $GET_SYNONYMS = array("get");
	protected static $CREATE_SYNONYMS = array("put", "create");
	protected static $UPDATE_SYNONYMS = array("post", "update");
	protected static $DELETE_SYNONYMS = array("delete");

	/**
	 * get data
	 *
	 * @param $request_item: an array($key=>$value)
	 * @return mix an array or some string
	 */
	public function fetch(request_item $request_item) {
		try {
			$method = strtolower($request_item->get_method());
			if(in_array($method, self::$GET_SYNONYMS)) {
				$response = $this->get($request_item);
			}
			elseif (in_array($method, self::$UPDATE_SYNONYMS)) {
				$response = $this->post($request_item);
			}
			elseif (in_array($method, self::$DELETE_SYNONYMS)) {
				$response = $this->delete($request_item);
			}
			elseif(in_array($method, self::$CREATE_SYNONYMS)) {
				$response = $this->put($request_item);
			}
			else {
				throw new data_exception('TOKEN_ERROR', response_error::$FORBIDDEN);
			}
		}
		catch (data_exception $e) {
			$response = new response_item($e->getCode(), $e->get_message());
		}
		catch (Exception $e) {
			$response = new response_item(response_error::$INTERNAL_ERROR, $e->get_message());
		}
		return $response;
	}

	abstract public function delete(request_item $request_item);
	abstract public function get(request_item $request_item);
	abstract public function post(request_item $request_item);
	abstract public function put(request_item $request_item);
}